<?php
/**
 * 用户中心
 * 作者：MYL
 */
namespace app\api\controller;

use app\api\model\UserModel;
use think\Db;

class Users extends Base
{
    /**
     * 获取用户信息
     */
    public function user_details()
    {
        if ($this->request->isPost()) {
            $user_id = I('post.user_id');
            $row = M('users')->field('user_code,reg_time,provincial_jane,head_pic,nickname,mobile,email,company,scale,demand')->where('user_id',$user_id)->find();
            $row['reg_time'] = date('Ymd',$row['reg_time']);
            if ($row) {
                ajaxReturn(['status' => 1, 'msg' => 'Success！', 'result' => $row]);
            } else {
                ajaxReturn(['status' => -1, 'msg' => '获取失败！']);
            }
        } else {
            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
        }
    }

    /**
     * 完善资料
     * 用户单个修改用户名称和单位
     */
    public function user_name_company()
    {
        $user_id = I('post.user_id');
        $nickname = I('post.nickname');
        $company = I('post.company'); //公司单位
        $scale = I('post.scale'); //企业规模
        $demand = I('post.demand'); //业务需求

        $where['user_id'] = $user_id;
        if (!empty($nickname)) {
            $data['nickname'] = $nickname;
        }
        if (!empty($company)) {
            $data['company'] = $company;
        }
        if (!empty($scale)) {
            $data['scale'] = $scale;
        }
        if (!empty($demand)) {
            $data['demand'] = $demand;
        }
        $user = UserModel::modify($where,$data);
        if ($user) {
            ajaxReturn(['status' => 1, 'msg' => 'Success！']);
        } else {
            ajaxReturn(['status' => -1, 'msg' => 'Errors Or Duplication！']);
        }
    }

    /**
     * 用户签到（签到有积分=2）
     */
    public function userSign(){
        if ($this->request->isPost()) {
            $data['user_id'] = I('post.user_id');
            $data['sign_last'] = time();
            $data['cumtrapz'] = 2;

            $row = UserModel::signAdd($data['user_id'],$data); //签到 + 增加积分
            if ($row==1) {
                ajaxReturn(['status' => 1, 'msg' => '今天已签到！']);
            } elseif($row==2) {
                $mag['user_id'] = $data['user_id'];
                $mag['pay_points'] = 2;
                $mag['change_time'] = time();
                $mag['order_sn'] = '';
                $mag['order_id'] = 0;
                $mag['desc'] = '签到送积分';
                M('account_log')->insert($mag);
                ajaxReturn(['status' => 1, 'msg' => '签到成功！']);
            } else {
                ajaxReturn(['status' => -1, 'msg' => '签到失败，请重新签到！']);
            }
        } else {
            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
        }
    }

    /**
     * 用户的总积分及连签的总数
     */
    public static function signContinuity(){
        $user_id = I('post.user_id');

        $rs = M('users')->field('pay_points')->where('user_id',$user_id)->find();
        $data['pay_points'] = $rs['pay_points'];
        $row = M('user_sign')->field('sign_count')->where('user_id',$user_id)->find();
        if (empty($row)) {
            $data['sign_count'] = 0;
        } else {
            $data['sign_count'] = $row['sign_count'];
        }
        ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $data]);
    }

//    /**
//     * 设置支付密码 and 重置密码
//     */
//    public function paymentPassword(){
//        if ($this->request->isPost()) {
//            $user_id = I("post.user_id");
//            $password = encrypt(I('post.password')); //新密码
//            $mobile = I('post.mobile');
//            $code  = I('post.code');
//            $type = I('post.type'); //操作类型 1.设置密码 2.修改密码
//            $newPassword = encrypt(I('post.encrypt_password')); //旧密码
//
//            //检测是手机号的格式
//            if (!check_mobile($mobile)) {
//                ajaxReturn(['status' => -1, 'msg' => '手机号码格式错误！']);
//            }
//            $mobile = session('mobile'); $codes = session('code'); //从session中取回验证码和手机号
//            if (!($code == $codes)) {
//                ajaxReturn(['status' => -1, 'msg' => '验证码错误，请重新输入或重新获取！']);
//            }
//            if (!($mobile == $mobile)) {
//                ajaxReturn(['status' => -1, 'msg' => '该手机号码和获取验证码的手机号码不一致，请重新输入！']);
//            }
//            session('mobile', null);session('code', null); // 清除session
//            if ($type == 1) {
//                $user = UserModel::setPaymentPassword($user_id,$password,$newPassword,$mobile,$type);
//            }
//            if ($type==2) {
//                $user = UserModel::setPaymentPassword($user_id,$password,$newPassword,$mobile,$type);
//            }
//            if ($user == 1) {
//                ajaxReturn(['status' => 1, 'msg' => 'Success！']);
//            } elseif ($user == 0) {
//                ajaxReturn(['status' => -1, 'msg' => '设置失败，请重新设置支付密码！']);
//            } elseif ($user == 2) {
//                ajaxReturn(['status' => -1, 'msg' => '设置失败，新密码和旧密码一致！']);
//            } elseif ($user==3) {
//                ajaxReturn(['status' => -1, 'msg' => '设置失败，该手机号码和获取验证码手机号码不一致！']);
//            } else {
//                ajaxReturn(['status' => -1, 'msg' => '设置失败，新密码不能和旧密码一样！']);
//            }
//        } else {
//            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
//        }
//    }

    /**
     * 设置支付密码 and 重置密码
     */
    public function paymentPassword()
    {
        if ($this->request->isPost()) {
            $user_id = I("post.user_id");
            $password = encrypt(I('post.password')); //密码
            $type = I('post.type'); //操作类型 1.设置密码 2.修改密码

            $paypwd = Db::name('users')->field('paypwd')->where('user_id',$user_id)->find();
            if ($paypwd['paypwd'] == $password) {
                ajaxReturn(['status' => -1, 'msg' => '新密码不能和旧密码一致！']);
            }
            //新增密码
            if($type==1) {
                $rs = Db::name('users')->where('user_id',$user_id)->update(['paypwd'=>$password]);
                if (!empty($rs)) {
                    ajaxReturn(['status' => 1, 'msg' => '设置成功！']);
                } else {
                    ajaxReturn(['status' => -1, 'msg' => '设置失败！']);
                }
            }
            //修改密码
            if($type==2){
               if (!empty($paypwd)) {
                   $rs = Db::name('users')->where('user_id',$user_id)->update(['paypwd'=>$password]);
                   if (!empty($rs)) {
                       ajaxReturn(['status' => 1, 'msg' => '修改成功！']);
                   } else {
                       ajaxReturn(['status' => -1, 'msg' => '修改失败！']);
                   }
               } else {
                   ajaxReturn(['status' => -1, 'msg' => '您还没有支付密码，请先设置密码！']);
               }
            }
        } else {
            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
        }
    }

    /**
     * 判断用户是否设置余额支付密码
     */
    public function userIsPwd()
    {
        if ($this->request->isPost()) {
            $user_id = I("post.user_id");
            $submitPrice = I("post.submitPrice");

            $where['paypwd'] = 0;
            $where['user_id'] = $user_id;
            //判断用户是否设置密码
            $userIsPwd = Db::name('users')->field('user_money')->where($where)->find();
            if (!empty($userIsPwd)) {
                ajaxReturn(['status' => -1, 'msg' => '您还没有设置余额支付密码！']);
            } else {
                $rs = Db::name('users')->field('user_money')->where('user_id',$user_id)->find();
                if ($rs['user_money']>=$submitPrice) {
                    ajaxReturn(['status' => 1, 'msg' => 'ok！']);
                } else {
                    ajaxReturn(['status' => -1, 'msg' => '您的余额不足，请充值或选择别的支付方式！','ss'=>$userIsPwd]);
                }
            }
        } else {
            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
        }
    }

    /**
     * 积分明细
     */
    public function pointsDetails()
    {
        $user_id = I('post.user_id');
        $page = I('page','1');

        $rs = Db::name('account_log')->field("FROM_UNIXTIME(change_time,'%Y-%m-%d %H:%i:%s') as change_time,desc,pay_points")
            ->where('user_id',$user_id)->where('desc','like','%积分%')
            ->page($page, '10')->order('log_id desc')->select();
        if ($rs) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $rs]);
        } else {
            ajaxReturn(['status' => 1, 'msg' => '获取失败！', 'result' => []]);
        }
    }

    /**
     * 用户建议反馈
     */
    public function feedback()
    {
        if ($this->request->isPost()) {
            $data['user_id'] = I("post.user_id");
            $data['title'] = I("post.title");
            $data['describe'] = I("post.describe");
            $data['mobile'] = I("post.mobile");
            $data['mailbox'] = I("post.mailbox");
            $data['add_time'] = time();
            //查询用户的名称
            $user = Db::name('users')->where('user_id',I("post.user_id"))->field('nickname')->find();
            $data['user_name'] = $user['nickname'];
            $rs = Db::name('user_feedback')->insert($data);
            if (!empty($rs)) {
                ajaxReturn(['status' => 1, 'msg' => '提交成功，请耐心等待！']);
            } else {
                ajaxReturn(['status' => -1, 'msg' => '提交有误，请重新提交建议！']);
            }
        } else {
            ajaxReturn(['status' => -1, 'msg' => '请填写数据！']);
        }
    }


    /**-------------------------------------我的充值模块-------------------------------------------------------------*/
    /**
     * 我的消费记录列表
     * 按日期查询（每个月的消费）
     */
    public function consumptionRecord()
    {

    }

    /**
     * 我的(余额)和我(未支付的订单总额)和(消费总额)
     */
    public function balanceNot()
    {
        $user_id = I('post.user_id');

        $userMoney = Db::name('users')->field('user_money')->where('user_id',$user_id)->find(); //余额
        $notOrderMoney = Db::name('order')->field('sum(order_amount) as orderAmount')->where(['user_id'=>$user_id,'pay_status'=>0])->find(); //未支付订单总价格
        $orderMoney = Db::name('order')->field('sum(order_amount) as orderAmount')->where(['user_id'=>$user_id,'pay_status'=>1])->find(); //已消费订单总价格
        if (!empty($userMoney['user_money'])) {
            $userMoney = $userMoney['user_money'];
        } else {
            $userMoney = '0.00';
        }
        if (!empty($notOrderMoney["orderAmount"])) {
            $notOrderMoney2 = $notOrderMoney["orderAmount"];
        } else {
            $notOrderMoney2 = '0.00';
        }
        if (!empty($orderMoney['orderAmount'])) {
            $orderMoney2 = $orderMoney['orderAmount'];
        } else {
            $orderMoney2 = '0.00';
        }
        ajaxReturn(['status' => 1, 'msg' => 'ok！', 'userMoney' => $userMoney, 'notOrderMoney'=>$notOrderMoney2, 'orderMoney'=>$orderMoney2]);
    }


    /**
     * 充值余额（提交订单）
     */
    public function rechargeBalance()
    {
        $user_id = I('post.user_id');
        $data['account'] = I('post.subMoney');
        $data['pay_name'] = I('post.pay_code');
        $data['order_sn'] = UserModel::get_order_sn();
        $data['ctime'] = time();
        $data['pay_status'] = 0;
        $user = M('users')->field('nickname')->where('user_id',$user_id)->find();
        $data['user_id'] = $user_id;
        $data['nickname'] = $user['nickname'];

        $rechargeId = Db::name('recharge')->insertGetId($data);
        if (!empty($rechargeId)) {
            $recharge = M('recharge')->field('order_sn,account')->where('order_id',$rechargeId)->find();
            ajaxReturn(['status' => 1, 'msg' => 'Success！', 'result' => $recharge]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => 'Error！']);
        }
    }

}